<template>
	<el-input :disabled="true" v-model="componentVal" />
</template>

<script lang="ts" setup>
import { TablePageType, TabFieldType, timestampType } from '/@/components/Linxyun/custom/DataStructs/commontype';
import { ElMessage } from 'element-plus';

const props = withDefaults(
	defineProps<{
		pageInfo?: TablePageType;
		filterIdx: number;
		located: string;
		pageData?: any;
		inputData: Record<string, any>;
		globalData?: any;
		modelValue: string;
		FieldItem: TabFieldType | null;
	}>(),
	{
		located: 'table',
		filterIdx: -1,
		pageData: {},
		globalData: null,
		modelValue: '',
		FieldItem: null,
	}
);

const { FieldItem, located, inputData, pageData, globalData } = toRefs(props);
const emit = defineEmits(['update:modelValue', 'exportData']);

const state = reactive({
	componentVal: '',
});

const { componentVal } = toRefs(state);
const init = () => {
	if (['Edit', 'Detail'].includes(located.value)) componentVal.value = props.modelValue ?? '';
	else if (['Add'].includes(located.value)) {
		const SequenceConfig = FieldItem.value?.SequenceConfig;
		if (SequenceConfig?.sequenceName && SequenceConfig?.prefix && SequenceConfig?.time) {
			let num = 0;
			const timestamp = getTimestamp(SequenceConfig!.time);
			if (timestamp === null) return ElMessage.error('时间戳生成失败');

			componentVal.value = SequenceConfig?.prefix + timestamp + num;
			emit('update:modelValue', componentVal.value);
		} else {
			ElMessage.error('序列配置参数异常');
		}
	}
};
init();
/**
 * 获取时间戳
 * @param type
 */
function getTimestamp(type: timestampType) {
	if (type === timestampType.second) {
		return Math.floor(Date.now() / 1000);
	} else if (type === timestampType.msec) {
		return Date.now();
	}
	return null;
}
</script>
